<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>账号注册-党支部论坛</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <style>
        body{
            background-image:url("../../images/login/forget.jpg");
            background-size:cover;
        }
        #wrapper{
            width: 1250px;
            height: 600px;
            margin: 0 auto;
        }
        #header{
            width: 100%;
            height: 100px;
        }
        #main{
            background-color: rgba(96,96,96,0.3);
            margin-left: 350px;
            margin-top: 50px;
            padding-top: 20px;
            width: 500px;
            height: 300px;
            border-radius: 10px;
            text-align: center;
        }
        #send{
            width:100px;
            height:35px;
        }

        #submit{
            background-color: dodgerblue;
            border-radius: 5px;
            border: none;
            width: 260px;
            height: 40px;
            color: aliceblue;
        }
    </style>
</head>
<body>
<div id="wrapper">
    <div id="header"></div>
    <div id="main">
        <br><br>
        <form action="mailLog.do" method="post">
            <input type="text" placeholder="请输入邮箱："th:value="${mailname}"name="mailname" id="mailname" onclick="onmail()" onblur="check()" style="height:30px;width:250px;"><br>
            <span id="email"></span><br>
            <input type="text" placeholder="请输入验证码："style="height:30px;width:150px;" name="varCode" id="varCode" >
            <input type="button" value="获取验证码" onclick="sendMail()" id="send"><br>
            <spen th:text="${error}" id="error"></spen><br><br>
            <input type="submit" value="提交" id="submit" >
        </form>
    </div>
</div>
    <script type="text/javascript">
        $('#send').attr("disabled",true);
        function onmail() {
            $("#email").html("");
        }
        function check() {
            $("#error").text("");
            var mailname = $("#mailname").val();
            $.post("check.do",{mailname:mailname}, function (data) {
                if(data.loginError=="F"){
                    var str = '<a href="remember.do">去登录</a>';
                    $("#email").html("该邮箱存在，"+str);
                } else if(data.loginError=="OK"){
                    if(mailname==""){
                        $("#email").html("请输入邮箱");
                    }else if(mailname.indexOf("@qq.com")==-1){
                        $("#email").html("请输入正确的邮箱");
                    }else {
                        $("#email").html("");
                    }
                }
                var mailchack = $("#email").text();
                if(mailname!=""&&(mailchack.indexOf("请输入")==-1&&mailchack.indexOf("该邮箱存在")==-1)){
                    $('#submit').attr("disabled",false);
                    $('#send').attr("disabled",false);
                } else {
                    $('#send').attr("disabled",true);
                    $('#submit').attr("disabled",true);
                }
            })
        }
        function sendMail() {
            var mailname = $("#mailname").val();
            var btn = $("#send");
            var countdown = 60;//设置时间
            var content = "欢迎您注册，您的验证码为（五分钟有效）：";
            $.post("sendMail.do", {mailname: mailname,content:content}, function settime() {
                //当时间为0的时候改变button的样式
                if (countdown == 0) {
                    btn.attr("disabled", false);
                    btn.attr('value',"获取验证码");
                    btn.removeClass("disabled");
                    countdown = 60;
                    return;
                } else {
                    btn.addClass("disabled");
                    btn.attr("disabled", true);
                    btn.attr('value',"重新发送(" + countdown + ")");
                    countdown--;
                }
                setTimeout(settime, 1000);//每秒钟执行一次settime函数
            });
        }

    </script>
</body>
</html>